/**
 * Created by zhangyilin on 2018/11/10.
 */
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/***
 *
 * 学生进行注册
 */

// 扩展 HttpServlet 类
public class addUser extends HttpServlet{

    private String message;

    public void init() throws ServletException
    {
        //
        // 执行必需的初始化
        message = "Hello World";
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setContentType("text/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        response.setCharacterEncoding("UTF-8");//setContentType与setCharacterEncoding的顺序不能调换，否则还是无法解决中文乱码的问
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String identity = "学生";
//        out.println(account+password+name+identity);
        String answer = "0";
        try {
            //写数据库访问
            Class.forName("com.mysql.jdbc.Driver");
            Connection connect = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/ListSystem", "root", "");
            Statement stmt = connect.createStatement(); //创建Statement对象
            String sql;
            sql = "INSERT INTO User_t (UserID, UserName, UserPass,Identity) VALUES ('"+account+"', '"+name+"', '"+password+"','"+identity+"')";

            if (stmt.executeUpdate(sql) == 1){
                answer="1";
            }
//            out.println(stmt.executeUpdate(sql));
            out.write(answer);
            out.close();
            stmt.close();
            connect.close();
        }catch (Exception e) {
            out.print("get data error!");
            e.printStackTrace();
        }
    }

    public void destroy()
    {
        // 什么也不做
    }
}
